﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NginxServiceHelper
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private int _ServerState = 0;
        Common.NginxService service = null;

        public string ServerName
        {
            get
            {
                return service == null ? "" : service.ServiceName;
            }
            set { }
        }

        public MainWindow()
        {
            InitializeComponent();
            Init();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            try
            {
                service = new Common.NginxService();
                service.Main2();
                txtServerName.Text = service.ServiceName;
                ShowStatus();
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteLine("初始化失败");
                NewLife.Log.XTrace.WriteException(ex);
            }
        }
        /// <summary>
        /// 0:未安装，2：安装未运行，1：运行中
        /// </summary>
        public int ServerState { get => _ServerState; set => _ServerState = value; }
        /// <summary>
        /// 未安装
        /// </summary>
        public Visibility InstallState
        {
            get
            {
                return _ServerState == 0 ? Visibility.Visible : Visibility.Collapsed;
            }
            set
            { }
        }
        public Visibility UnInstallState
        {
            get
            {
                return _ServerState != 0 ? Visibility.Visible : Visibility.Collapsed;
            }
            set
            { }
        }
        public Visibility StartState
        {
            get
            {
                return _ServerState == 2 ? Visibility.Visible : Visibility.Collapsed;
            }
            set
            { }
        }
        public Visibility StopState
        {
            get
            {
                return _ServerState == 1 ? Visibility.Visible : Visibility.Collapsed;
            }
            set
            { }
        }

        private void ShowStatus()
        {
            try
            {
                if (service == null)
                {
                    NewLife.Log.XTrace.WriteLine("服务未初始化");
                    return;
                }
                
                int state = service.GetStatus();
                //btnInsert.Enabled = false;
                this.Dispatcher.Invoke(new Action(() =>
                {
                    try
                    {
                        ServerState = state;
                        if (state == 0)
                        {
                            lbState.Content = "服务未安装";
                            lbState.Background = Brushes.DarkGray;
                        }
                        else
                        {
                            switch (state)
                            {
                                case 1:
                                    lbState.Content = "服务正在运行...";
                                    lbState.Background = Brushes.Green;
                                    break;
                                case 2:
                                    lbState.Content = "服务未运行";
                                    lbState.Background = Brushes.Red;
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    catch { }
                }));
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        private void btnInstall_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                service.Install();
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteLine("服务安装失败");
                NewLife.Log.XTrace.WriteException(ex);
            }
            ShowStatus();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                service.Start();
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteLine("服务启动失败");
                NewLife.Log.XTrace.WriteException(ex);
            }
            ShowStatus();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                service.Stop();
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteLine("服务停止失败");
                NewLife.Log.XTrace.WriteException(ex);
            }
            ShowStatus();
        }

        private void btnUnInstall_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                service.UnInstall();
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteLine("服务卸载失败");
                NewLife.Log.XTrace.WriteException(ex);
            }
            ShowStatus();
        }
    }
}
